// Copyright 2011 Jefferson Willian
// willmont7@gmail.com

#include <cstdio>
#include <algorithm>

#define MAX 10000

using std::sort;

typedef struct Letter {
  int letter;
  int freq;
}Letter;

bool compare(Letter a, Letter b) {
  if (a.freq == b.freq)
    return a.letter < b.letter;

  return a.freq > b.freq;
}

int getInd(Letter l[], char c) {
  c -= 'a';
  for (int i = 0;i < 26;i++)
    if (l[i].letter == c)
      return i;
}

int main() {
  int n, m;
  Letter l[26];
  char decrypt[26];
  char str[MAX];
  int games = 1;

  while (scanf("%d%d", &n, &m) && (n != 0 || m != 0)) {
    for (int i = 0;i < 26;i++) {
      l[i].letter = i;
      l[i].freq = 0;
    }

    scanf("%s", decrypt);

    getchar();

    scanf("%[^\n]", str);

    for (int i = 0;i < n;i++)
      if (str[i] != ' ')
        l[str[i]-'a'].freq++;

    sort(l, l+26, compare);

    printf("Teste %d\n", games++);

    for (int i = 0;i < n;i++) {
      if (str[i] == ' ')
        printf(" ");
      else
        printf("%c", decrypt[getInd(l, str[i])]);
    }

    printf("\n\n");
  }

  return 0;
}
